/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.pages.enterprise;

import java.util.Arrays;
import java.util.List;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.eelpo.cmmp.core.exception.ServiceException;
import com.eelpo.cmmp.core.wicket.component.label.FeedbackLabel;
import com.eelpo.cmmp.core.wicket.page.extensions.SimpleWebPage;
import com.eelpo.cmmp.uac.dao.vo.Enterprise;
import com.eelpo.cmmp.uac.dao.vo.enumed.Status;
import com.eelpo.cmmp.uac.services.IEnterpriseManageService;
import com.eelpo.common.util.BlankUtils;

/**
 * <pre>
 * 
 * 新增和修改企业页面
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class AddOrEditEnterprise extends SimpleWebPage {

	@SpringBean
	protected IEnterpriseManageService enterpriseManageService;

	/**
	 * 
	 * 新增企业构造函数
	 * 
	 */
	public AddOrEditEnterprise() {
		this("[新增]企业", new Enterprise(Status.ENABLED));
	}

	/**
	 * 修改企业构造函数
	 * 
	 * @param enterprise
	 *            企业
	 */
	public AddOrEditEnterprise(final Enterprise enterprise) {
		this("[修改]企业", enterprise);
	}

	/**
	 * @param title
	 *            页面标题
	 * @param enterprise
	 *            新增或修改的企业
	 */
	public AddOrEditEnterprise(String title, final Enterprise enterprise) {
		super(title);

		add(renderFormContainer(enterprise));
	}

	public Form<Enterprise> renderFormContainer(final Enterprise enterprise) {

		Form<Enterprise> form = new Form<Enterprise>("form", new Model<Enterprise>(enterprise));

		renderFeedbackPanel(form);
		renderEnterpriseParentTextField(form);
		renderEnterpriseNameTextField(form);
		renderEnterpriseIndexTextField(form);
		renderEnterpriseContactTextField(form);
		renderEnterprisePhoneTextField(form);
		renderEnterpriseDescriptionTextArea(form);
		renderEnterpriseStatusDropDownChoice(form);
		renderSaveButton(form);
		renderBackButton(form);
		return form;
	}

	public void renderFeedbackPanel(Form<Enterprise> form) {
		form.add(renderFeedbackPanel("feedback"));
	}

	public void renderEnterpriseParentTextField(Form<Enterprise> form) {
		form.add(new TextField<String>("txtParent", new PropertyModel<String>(form.getDefaultModelObject(), "name")).setMarkupId("txtParent"));
	}

	public void renderEnterpriseNameTextField(Form<Enterprise> form) {
		TextField<String> txtName = new TextField<String>("txtName", new PropertyModel<String>(form.getDefaultModelObject(), "name"));
		txtName.setMarkupId("txtName");
		form.add(new FeedbackLabel("txtNameFeedback", txtName));
		form.add(txtName);
	}

	public void renderEnterpriseIndexTextField(Form<Enterprise> form) {
		TextField<String> txtIndex = new TextField<String>("txtIndex", new PropertyModel<String>(form.getDefaultModelObject(), "index"));
		txtIndex.setMarkupId("txtIndex");
		form.add(new FeedbackLabel("txtIndexFeedback", txtIndex));
		form.add(txtIndex);
	}

	public void renderEnterpriseContactTextField(Form<Enterprise> form) {
		TextField<String> txtContact = new TextField<String>("txtContact", new PropertyModel<String>(form.getDefaultModelObject(), "contact"));
		txtContact.setMarkupId("txtContact");
		form.add(new FeedbackLabel("txtContactFeedback", txtContact));
		form.add(txtContact);
	}

	public void renderEnterprisePhoneTextField(Form<Enterprise> form) {
		TextField<String> txtPhone = new TextField<String>("txtPhone", new PropertyModel<String>(form.getDefaultModelObject(), "phone"));
		txtPhone.setMarkupId("txtPhone");
		form.add(new FeedbackLabel("txtPhoneFeedback", txtPhone));
		form.add(txtPhone);
	}

	public void renderEnterpriseDescriptionTextArea(Form<Enterprise> form) {
		form.add(new TextArea<String>("txtDescription", new PropertyModel<String>(form.getDefaultModelObject(), "description")));
	}

	public void renderEnterpriseStatusDropDownChoice(final Form<Enterprise> form) {
		List<Status> dataList = Arrays.asList(new Status[] { Status.ENABLED, Status.DISABLED });
		IModel<Status> model = new PropertyModel<Status>(form.getDefaultModelObject(), "status");
		IChoiceRenderer<Status> choiceRenderer = new ChoiceRenderer<Status>("label", "value");
		DropDownChoice<Status> enterpriseStatus = new DropDownChoice<Status>("selStatus", model, dataList, choiceRenderer);
		enterpriseStatus.setMarkupId("selStatus");
		form.add(new FeedbackLabel("selStatusFeedback", enterpriseStatus));
		form.add(enterpriseStatus);
	}

	public void renderSaveButton(final Form<Enterprise> form) {
		Button btnSave = new Button("btnSave") {
			private static final long serialVersionUID = 6855404939131434877L;

			public void onSubmit() {
				try {

					Enterprise enterprise = (Enterprise) form.getDefaultModelObject();

					if (BlankUtils.isBlank(enterprise.getId())) {
						enterpriseManageService.addEnterprise(enterprise);
					} else {
						enterpriseManageService.updateEnterprise(enterprise);
					}

					setResponsePage(new EnterpriseManage());
				} catch (ServiceException ex) {
					info(ex.getMessage());
				} catch (Exception e) {
					e.printStackTrace();
					info("保存失败！");
				}
			}
		};

		form.add(btnSave);
	}

	public void renderBackButton(final Form<Enterprise> form) {
		Link<Void> btnBack = new Link<Void>("btnBack") {

			private static final long serialVersionUID = -9129976099407032628L;

			@Override
			public void onClick() {
				setResponsePage(new EnterpriseManage());
			}
		};

		form.add(btnBack);
	}
}
